package com.android.fragmentdemo.fragments
import androidx.lifecycle.ViewModelProvider
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.android.fragmentdemo.R
import com.android.fragmentdemo.adapters.ListDataRvAdapter
import com.hengfoxwolf.commonbase.base.BaseFragment
import com.android.fragmentdemo.databeans.DataFactoryIp
import com.android.fragmentdemo.databinding.FragmentListDataBinding
import com.android.fragmentdemo.defineviews.ListDataItemDecoration

class ListDataFragment : BaseFragment<FragmentListDataBinding>() {

    private lateinit var viewModel: ListDataViewModel

    override fun getLayoutId() = R.layout.fragment_list_data

    override fun init(savedInstanceState: Bundle?) {
        arguments.let {
            val title = it?.getString(KEY_PARAM) ?: ""
            //mBinding.tvList.text = title
        }
        mBinding.rvListData.apply {
            layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
            addItemDecoration(ListDataItemDecoration(context).also {
                it.setDrawLastLine(false)
            })
            adapter = ListDataRvAdapter().apply {
                //setItemDataSet(DataFactoryIp.createItemData())
                setItemDataSet(DataFactoryIp.createItemListData())
            }
            //缓存所有item,解决动态添加子内容,item滑动出现错乱的问题.
            setItemViewCacheSize(adapter!!.itemCount)
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewModel = ViewModelProvider(this)[ListDataViewModel::class.java]
    }

    override fun onResume() {
        super.onResume()
        Log.d(TAG, "111--${TAG}--override fun onResume()")
    }

    companion object {
        private const val TAG = "ListDataFragment"
        private const val KEY_PARAM = "list_data"

        fun newInstance(param: String): ListDataFragment {
            val fragment = ListDataFragment()
            val bundle = Bundle()
            bundle.putString(KEY_PARAM, param)
            fragment.arguments = bundle
            return fragment
        }
    }
}